package t20250320.Wc;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.streaming.api.functions.source.RichSourceFunction;
import org.apache.flink.streaming.api.functions.source.SocketTextStreamFunction;
import org.apache.flink.streaming.api.functions.source.SourceFunction;
import org.apache.flink.util.Collector;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Wc {

    public static void main(String[] args) throws Exception {

        Configuration flinkConf = new Configuration();
        flinkConf.setString("rest.port","9091");
        final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(flinkConf);


        DataStreamSource<String> source = env.addSource(new RichSourceFunction<String>() {

            private boolean isRunning = true;
            @Override
            public void run(SourceContext<String> ctx) throws Exception {
                System.out.println("SourceContext:" + ctx);
                while (isRunning) {
                   // ctx.collect("hello,world");
                    Thread.sleep(1000);
                }
            }

            @Override
            public void cancel() {
                isRunning = false;
            }
        });


        SingleOutputStreamOperator<String> ssos = source.flatMap(new FlatMapFunction<String, String>() {
            @Override
            public void flatMap(String value, Collector<String> out) throws Exception {
                for (String s : value.split(",")) {
                    System.out.println("flatMap:" + out);
                    out.collect(s);
                }
            }
        }).setParallelism(2);

        ssos.map(new MapFunction<String, String>() {
            @Override
            public String map(String value) throws Exception {
                return value + "_heihei";
            }
        }).setParallelism(3).process(new ProcessFunction<String, ArrayList<String[]>>() {
            @Override
            public void processElement(String value, ProcessFunction<String, ArrayList<String[]>>.Context ctx, Collector<ArrayList<String[]>> out) throws Exception {
                ArrayList<String[]> list = new ArrayList<>(Arrays.asList(new String[]{"aa"}, new String[]{"aa"}));
                out.collect(list);
            }
        })

                .addSink(new MySink());




        env.execute("app");
    }
}
